import requests
import parsel
import json
from redis import StrictRedis


def main():
    url = 'https://www.bilibili.com/v/popular/rank/all'
    # 搜索网址
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}  # 伪装
    # 伪装爬虫
    r = requests.get(url, headers=headers)  # 请求网站
    body = r.content.decode('utf-8')
    sel = parsel.Selector(body)
    # print(body)
    name = sel.css('.title::text').getall()
    number = sel.css('.data-box::text').getall()
    link = sel.css('.title::attr(href)').getall()
    data_list = []
    play_list = []
    for i in range(1,len(number),3):
        play_list.append(str(number[i]).replace(" ","").replace("\n",""))
    for i in range(0, 10):
        data = {}
        data['name'] = name[i]
        data['number'] = play_list[i]
        data['link'] = link[i].replace("//", "https://")
        data_list.append(data)
        print(data)
    redis = StrictRedis(host='localhost', port=6379, decode_responses=True)
    save_redis(redis, data_list)


def save_redis(redis, data_list):
    redis.delete('bilibili_hot')
    for o in data_list:
        # s = json.dumps(o.__dict__, ensure_ascii=False)
        s = json.dumps(o, ensure_ascii=False)
        print(s)
        redis.rpush('bilibili_hot', s)


if __name__ == '__main__':
    main()
